module SynAn where

import qualified Data.Map as M
import qualified Data.Set as S
import Data.List
import Data.Maybe

type Symbol = String
type State = String 
type Lexeme = (String, String)
type Action = (Char, String, [Lexeme]) -> (Char, String, [Lexeme]) 
type Transitions = M.Map (State, Symbol, StackSymbol) (State, StackSymbol [Action])

data FSM = FSM
  { symbols :: [Symbol]
  , start_state :: State
  , eos :: String
  , delta :: Transitions
  }

data Tree = Tree
  { root :: String
  , subtrees ::[Tree]  
  }

-------------------------------------------------------------
execFSM :: FSM -> [Lexeme] -> Tree 
execFSM (FSM syms s_state eos delta) str =  
  next s_state stack [] (Tree ) 0

  where
    next :: State -> String -> [Lexeme] -> Integer -> Tree
    next cur_state str stack lex num = 
      case n_state of
        "" -> error ("Error in lex " ++ (show num) ++ "!\n")
        otherwise -> 
          if end then n_lex
          else next n_state xs n_buf n_lex (num+1)

      where
        end = (str == [])
        (x:xs)
          | end = '_':[]
          | otherwise = str 
        symbol 
          | end = (cur_state, eos)
          | otherwise = (cur_state, x `find_set` syms)
        (n_state, actions) = fromMaybe ("", []) 
                            $ symbol `M.lookup` delta
        compos = foldl (.) id (reverse actions) 
        (_, n_buf, n_lex) = compos (x, buf, lex)



